package com.gollum.system.service.impl;

import com.gollum.common.context.PageContextHolder;
import com.gollum.common.domain.response.TableSupport;
import com.gollum.common.exception.BusinessException;
import com.gollum.common.utils.BeanUtils;
import com.gollum.common.utils.StringUtils;
import com.gollum.jpa.common.Specifications;
import com.gollum.jpa.domain.Page;
import com.gollum.jpa.domain.PageQueryRequest;
import com.gollum.jpa.service.JpaServerImpl;
import com.gollum.system.domain.dao.SysOperLogDao;
import com.gollum.system.domain.model.SysLogininfor;
import com.gollum.system.domain.model.SysOperLog;
import com.gollum.system.domain.request.SysOperLogRequest;
import com.gollum.system.domain.response.SysLogininforResponse;
import com.gollum.system.domain.response.SysOperLogResponse;
import com.gollum.system.service.SysOperLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * SysOperLogService 实现
 *
 * @author Herther
 * @version 1.0.0
 * @createTime 2022年08月26日 00:01:00
 */
@Service
public class SysOperLogServiceImpl extends JpaServerImpl<SysOperLog,Long> implements SysOperLogService {
    @Autowired
    private SysOperLogDao sysOperLogDao;

    /**
     * 构建列表查询参数
     *
     * @param sysOperLogRequest 前端请求的参数
     * @return jpa参数查询标准
     * @auther: Herther
     * @since 1.0.0
     * @date: 2022/9/17 21:08
     */
    public Specification<SysOperLog>  getParams(SysOperLogRequest sysOperLogRequest){
        Specification<SysOperLog> spec = Specifications.<SysOperLog>and()
                //id
                .eq(StringUtils.isNotNull(sysOperLogRequest.getId()),
                        SysOperLog::getId, sysOperLogRequest.getId())
                //模块编码
                .eq(StringUtils.isNotNull(sysOperLogRequest.getModuleCode()),
                        SysOperLog::getModuleCode, sysOperLogRequest.getModuleCode())
                //模块标题
                .like(StringUtils.isNotNull(sysOperLogRequest.getModuleTitle()),
                        SysOperLog::getModuleTitle, sysOperLogRequest.getModuleTitle())
                //操作类型
                .eq(StringUtils.isNotNull(sysOperLogRequest.getBusinessType()),
                        SysOperLog::getBusinessType, sysOperLogRequest.getBusinessType())
                //设备
                .eq(StringUtils.isNotNull(sysOperLogRequest.getOperatorType()),
                        SysOperLog::getOperatorType, sysOperLogRequest.getOperatorType())
                //操作人员
                .like(StringUtils.isNotNull(sysOperLogRequest.getUsername()),
                        SysOperLog::getUsername, sysOperLogRequest.getUsername())
                //请求地址
                .eq(StringUtils.isNotNull(sysOperLogRequest.getRequestUrl()),
                        SysOperLog::getRequestUrl, sysOperLogRequest.getRequestUrl())
                //操作地点
                .like(StringUtils.isNotNull(sysOperLogRequest.getLocation()),
                        SysOperLog::getLocation, sysOperLogRequest.getLocation())
                //操作主机地址
                .eq(StringUtils.isNotNull(sysOperLogRequest.getAddressIp()),
                        SysOperLog::getAddressIp, sysOperLogRequest.getAddressIp())
                //操作主机地址
                .like(StringUtils.isNotNull(sysOperLogRequest.getAddressIp()),
                        SysOperLog::getAddressIp, sysOperLogRequest.getAddressIp())

                //请求类型
                .eq(StringUtils.isNotNull(sysOperLogRequest.getRequestMethod()),
                        SysOperLog::getRequestMethod, sysOperLogRequest.getRequestMethod())

                //操作状态（0正常 1异常）
                .eq(StringUtils.isNotNull(sysOperLogRequest.getStatus()),
                        SysOperLog::getStatus, sysOperLogRequest.getStatus())
                //查询操作时间范围
                .between(getBetweenCondition(sysOperLogRequest.getBeginTime(),sysOperLogRequest.getEndTime())
                        ,SysOperLog::getOperatorTime,sysOperLogRequest.getBeginTime(),sysOperLogRequest.getEndTime())
                .build();
        return spec;
    }

    @Override
    public TableSupport<SysOperLogResponse> listPage(SysOperLogRequest sysOperLogRequest) {
        Page<SysOperLog> page = PageContextHolder.get().toPage();
        page = sysOperLogDao.findAllWithPage(page,getParams(sysOperLogRequest));
        return TableSupport.toTableResponse(page, BeanUtils.copyToList(page.getRecords(), SysOperLogResponse.class));
    }


    @Override
    public SysOperLogResponse getDetailById(Long id) {
        SysOperLog sysOperLog = this.getById(id);
        if (Objects.isNull(sysOperLog)){
            throw new BusinessException("查询不存在");
        }
        SysOperLogResponse sysOperLogResponse = new SysOperLogResponse();
        BeanUtils.copyBeanProp(sysOperLog, sysOperLogResponse);
        return sysOperLogResponse;
    }

    @Override
    public void removeByIds(List<Long> ids) {
        try{
            this.removeAllByIds(ids);
        }catch (Exception e){
            throw new BusinessException("删除的对象不存在");
        }
    }



}
